package com.ev.hr.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ev.custom.domain.ContentAssocDO;
import com.ev.custom.service.ContentAssocService;
import com.ev.custom.service.DictionaryService;
import com.ev.framework.config.ConstantForHr;
import com.ev.framework.il8n.MessageSourceHandler;
import com.ev.hr.vo.*;
import com.ev.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ev.hr.dao.ContractInfoDao;
import com.ev.hr.domain.ContractInfoDO;
import com.ev.hr.service.ContractInfoService;
import com.ev.framework.utils.R;
import com.ev.apis.model.DsResultResponse;
import com.google.common.collect.Maps;


@Service
public class ContractInfoServiceImpl implements ContractInfoService {
	@Autowired
	private ContentAssocService contentAssocService;
	@Autowired
	private ContractInfoDao contractInfoDao;
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private MessageSourceHandler messageSourceHandler;
	
	@Override
	public ContractInfoDO get(Long id){
		return contractInfoDao.get(id);
	}
	
	@Override
	public List<ContractInfoDO> list(Map<String, Object> map){
		return contractInfoDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return contractInfoDao.count(map);
	}
	
	@Override
	public int save(ContractInfoDO contractInfo){
		return contractInfoDao.save(contractInfo);
	}
	
	@Override
	public int update(ContractInfoDO contractInfo){
		return contractInfoDao.update(contractInfo);
	}

	@Override
	public int updateAll(ContractInfoDO contractInfo){
		return contractInfoDao.updateAll(contractInfo);
	}
	
	@Override
	public int remove(Long id){
		return contractInfoDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return contractInfoDao.batchRemove(ids);
	}

	@Override
	public R saveAndVerify(ContractInfoSaveParam saveParam){
		/**
		 * 先保存主表信息
		 */
		ContractInfoDO contractInfoDO = saveParam.getContractInfoDO();
		if(contractInfoDO.getId()!=null){
			contractInfoDao.update(contractInfoDO);
		}else{
			contractInfoDO.setStatus(0);
			contractInfoDao.save(contractInfoDO);
		}
		/**
		 * 先删除附件
		 */
		contentAssocService.removeByAssocIdAndType(new Long[]{contractInfoDO.getId()}, ConstantForHr.CONTRACT_INFO_CONTENT);
		/**
		 * 保存附件信息
		 */
		contentAssocService.saveListForEntity(contractInfoDO.getId(),saveParam.getContentAssocList(), ConstantForHr.CONTRACT_INFO_CONTENT);
		Map<String,Object>  result= new HashMap<>();
		result.put("id",contractInfoDO.getId());
		return R.ok(result);
	}

	@Override
	public Map<String, Object> getById(Long id) {
		/**
		 * 获取主体信息
		 */
		ContractInfoVO contractInfo = contractInfoDao.getById(id);
		/**
		 * 获取附件数据
		 */
		List<ContentAssocDO> contentAssocList = contentAssocService.list(new HashMap<String,Object>(2){{put("assocId",id);put("assocType",ConstantForHr.CONTRACT_INFO_CONTENT);}});
		/**
		 * 结果组装
		 */
		ContractInfoDetail contractInfoSaveParam = new ContractInfoDetail();
		contractInfoSaveParam.setContractInfoDO(contractInfo);
		contractInfoSaveParam.setContentAssocList(contentAssocList);
		if (contractInfo != null) {
			Map<String,Object> result = Maps.newHashMap();
			result.put("data",contractInfoSaveParam);
			return R.ok(result);
		}
		return R.error();

	}

	@Override
	public Map<String, Object> pageList(ContractInfoPageParam pageParam) {
		Map<String,Object> result = Maps.newHashMap();

		JSONObject o = (JSONObject) JSONObject.toJSON(pageParam);
		o.putAll(roleService.getDataPermission(3));


		List<Map<String, Object>> data = contractInfoDao.pageList(o);
		int total = contractInfoDao.pageCount(o);
		result.put("data", new DsResultResponse(pageParam.getPageno(),pageParam.getPagesize(),total,data));
		return R.ok(result);
	}

	@Override
	public R removeCategory(Long categoryId) {
		Map<String,Object> param = new HashMap(){{put("contractCategory",categoryId);}};
		List<ContractInfoDO> contractInfoDOList = list(param);
		if(contractInfoDOList.size()>0){
			return R.error(messageSourceHandler.getMessage("common.dailyReport.batchRemove",null));
		}else{
			dictionaryService.remove(categoryId);
		}
		return R.ok();
	}

}
